package com.yufei.infoExtractor.core;

import java.io.IOException;




import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yufei.infoExtractor.io.AbstractHttpDataRetriever;
import com.yufei.infoExtractor.io.HttpDataRetriever;
import com.yufei.infoExtractor.io.DataRetrieverFactory;
import com.yufei.infoExtractor.io.DataRetrieverFeatures;
import com.yufei.infoExtractor.pfw.entity.Relatedlink;
import com.yufei.infoExtractor.util.CommonUtil;
import com.yufei.infoExtractor.util.PatternUtils;
import com.yufei.infoExtractor.util.URLUtil;

/**
 * @author zhaoyufei 分页链接类型的处理主要是处理某个信息实体的集合属性
 * 
 */
public class LinkActionForPagination implements LinkAction {
	private static Log mLog = LogFactory.getLog(LinkActionForPagination.class);

	public static Integer defaultpageNumber = 3;

	public void execute(Relatedlink link, Map context) throws Exception {
		// TODO Auto-generated method stub
		// pageUrlRegex

		this.pageAction(link, context);
		//when action finished 
		Integer currentPageNumber = (Integer) context.get("currentPageNumber");
		String currentPageContent = (String)context.get("currentPageContent");
        if(currentPageNumber!=null){
			context.remove(currentPageNumber);
		}
		if(currentPageContent!=null){
			context.remove(currentPageContent);
		}

	}

	/**
	 * @param link
	 * @param context
	 * @throws Exception
	 */
	private void pageAction(Relatedlink link, Map context) throws Exception {

		Integer currentPageNumber = (Integer) context.get("currentPageNumber");
		if (currentPageNumber == null) {
			context.put("currentPageNumber", 1);
			currentPageNumber = (Integer) context.get("currentPageNumber");
		} else {
			context.put("currentPageNumber", currentPageNumber + 1);
		}
		extracted(link, context);

		if (currentPageNumber == (defaultpageNumber - 1)) {
			return;

		}
		pageAction(link, context);

	}

	private void extracted(Relatedlink link, Map context)
			throws MalformedURLException, IOException, IllegalAccessException,
			InvocationTargetException {
		String pageUrlRegex = (String) context.get("pageUrlRegex");
		// currentCollectionName
		// currentContentRegex
		String currentCollectionName = (String) context
				.get("currentCollectionName");
		String currentContentRegex = (String) context
				.get("currentContentRegex");
		String currentPageContent = null;
		mLog.debug(currentPageContent);

		// 判断currentPageContent存在与否，如果存在则表示不是第一次递归，如果不存在则表示第一次递归
		// 如果第一次递归，必须设置currentPageContent属性
		boolean isFirstRecursion = context.get("currentPageContent") == null;
		if (isFirstRecursion) {
			String htmlContent = (String) context.get("currentHtmlContent");
			context.put("currentPageContent", htmlContent);
		} else {
			currentPageContent = (String) context.get("currentPageContent");
			/*if (mLog.isDebugEnabled()) {
				mLog.debug("开始对网页内容：" + currentPageContent + "进行分页提取信息");

			}*/
			String pageUrl = PatternUtils.getStrByRegex(currentPageContent,
					pageUrlRegex,false);
			pageUrl=pageUrl.trim();

			if (mLog.isDebugEnabled()) {
				mLog.debug("根据正则表达式：" + pageUrlRegex + "匹配网页得到的下一页Url地址是："
						+ pageUrl);

			}
			if (pageUrl == null) {
				return;
			}
			pageUrl = CommonUtil.normalizeUrl(pageUrl,
					CommonUtil.getBaseUrl(link.getLink()));
			String protocolType = (String) context.get("protocolType");
			Boolean requestExecuteJs = (Boolean) context
					.get("requestExecuteJs");
			if (requestExecuteJs == null) {
				requestExecuteJs = true;
			}
			
			currentPageContent = CommonUtil.getHtmlContent(context, pageUrl);
			currentPageContent=CommonUtil.removeSpace(currentPageContent);
			if (mLog.isDebugEnabled()) {
				mLog.debug("请求" + pageUrl + "得到的网页内容是：" + currentPageContent);

			}
			context.put("currentPageContent", currentPageContent);

		}
		// 设置value
		currentPageContent = (String) context.get("currentPageContent");

		List<String> values = PatternUtils.getListStrByRegex(
				currentPageContent, currentContentRegex);
		if (mLog.isDebugEnabled()) {
			if (values.size() == 0)
				mLog.debug("根据正则表达式：" + currentContentRegex + "匹配网页错误！");

		}

		for (String temp : values) {
			//目前暂且的约定：翻书需要分页获取的属性都以s结尾并且该实体类必须新增一个方法方法名是:add+属性名（第一个字符大写）
			CommonUtil.getMethod(link.getClass(),
					"add" + CommonUtil.upFirstChar(currentCollectionName))
					.invoke(link, temp);
		}
	}
}
